package com.lst.projectlib.component.CustomView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lst.projectlib.R;
import com.lst.projectlib.util.DensityUtil;
import com.lst.projectlib.util.SystemUtil;

/**
 * Created by Administrator on 2016/11/10.
 */

public class TopTitleBar extends RelativeLayout {
    final int BACK_ICON_WIDTH_DEFAULT = 11;
    final int BACK_ICON_HEIGHT_DEFAULT = 20;
    final int FINISH_ICON_WIDTH_DEFAULT = 22;
    final int FINISH_ICON_HEIGHT_DEFAULT = 22;
    final int ICON_PADDING = 5;

    final int[] ATTRS = new int[]{android.R.attr.background};//system attrs

    View vStatusBar,layNavMain;
    TextView tvNavBack,tvNavFinish,tvNavShare,tvNavTitle;

    public TopTitleBar(Context context) {
        this(context,null);
    }

    public TopTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //从xml的属性中获取属性值
        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.TopTitleBar);
        Drawable leftIcon = ta.getDrawable(R.styleable.TopTitleBar_yobn_backIcon);
        Drawable moreIcon = ta.getDrawable(R.styleable.TopTitleBar_yobn_moreIcon);
        Drawable finishIcon = ta.getDrawable(R.styleable.TopTitleBar_yobn_finishIcon);
        Drawable shareIcon = ta.getDrawable(R.styleable.TopTitleBar_yobn_shareIcon);
        int backIconWidth = ta.getDimensionPixelSize(R.styleable.TopTitleBar_yobn_backIconWidth, DensityUtil.dip2px(context,BACK_ICON_WIDTH_DEFAULT));
        int backIconHeight = ta.getDimensionPixelSize(R.styleable.TopTitleBar_yobn_backIconHeight,DensityUtil.dip2px(context,BACK_ICON_HEIGHT_DEFAULT));
        int finishIconWidth = ta.getDimensionPixelSize(R.styleable.TopTitleBar_yobn_finishIconWidth,DensityUtil.dip2px(context,FINISH_ICON_WIDTH_DEFAULT));
        int finishIconHeight = ta.getDimensionPixelSize(R.styleable.TopTitleBar_yobn_finishIconHeight,DensityUtil.dip2px(context,FINISH_ICON_HEIGHT_DEFAULT));
        String title = ta.getString(R.styleable.TopTitleBar_yobn_title);
        boolean bottomLineShow = ta.getBoolean(R.styleable.TopTitleBar_yobn_bottomLineShow,false);
        boolean statusBarSpaceShow = ta.getBoolean(R.styleable.TopTitleBar_yobn_statusBarSpaceShow,false);
        int statusBarSpaceColor = ta.getColor(R.styleable.TopTitleBar_yobn_statusBarSpaceColor, getResources().getColor(android.R.color.transparent));
        ta.recycle();

        // get system attrs (android.R.attr.background)
        ta = context.obtainStyledAttributes(attrs, ATTRS);
        int backgroundColor = ta.getColor(0, getResources().getColor(android.R.color.white));
        ta.recycle();

        //获取到控件
        //加载布局文件，与setContentView()效果一样
        LayoutInflater.from(context).inflate(R.layout.top_title_bar,this);
        vStatusBar = findViewById(R.id.vStatusBar);
        layNavMain = findViewById(R.id.layNavMain);
        View vNavDivider = findViewById(R.id.vNavDivider);
        tvNavBack = (TextView) findViewById(R.id.tvNavBack);
        tvNavFinish = (TextView) findViewById(R.id.tvNavFinish);
        tvNavShare = (TextView) findViewById(R.id.tvNavShare);
        tvNavTitle = (TextView) findViewById(R.id.tvNavTitle);
        View v_back_divider = findViewById(R.id.v_back_divider);

        setStatusBarSpaceShow(statusBarSpaceShow,statusBarSpaceColor);
        //将控件与设置的xml属性关联
        layNavMain.setBackgroundColor(backgroundColor);

        if(leftIcon!=null){
            leftIcon.setBounds(0, 0, backIconWidth, backIconHeight);
            tvNavBack.setCompoundDrawables(leftIcon, null, null, null);
            v_back_divider.setVisibility(View.GONE);
        }
        if(finishIcon!=null){
            finishIcon.setBounds(0, 0, finishIconWidth, finishIconHeight);
            tvNavFinish.setCompoundDrawables(null, null, finishIcon, null);
        }
        if(shareIcon!=null){
            shareIcon.setBounds(0, 0, finishIconWidth, finishIconHeight);
            tvNavShare.setCompoundDrawables(null, null, shareIcon, null);
            tvNavShare.setVisibility(VISIBLE);
        }

        tvNavTitle.setText(title);

        if(moreIcon!=null){
            moreIcon.setBounds(0, 0, backIconWidth, backIconHeight);
            tvNavTitle.setCompoundDrawables(null, null, moreIcon, null);
            tvNavTitle.setCompoundDrawablePadding(DensityUtil.dip2px(context,ICON_PADDING));
        }

        vNavDivider.setVisibility(bottomLineShow ? VISIBLE : GONE);

        reSizeTitleViewWidth();
    }

    private void reSizeTitleViewWidth(){
        //大概预估的值,后期再做修整
        if(!TextUtils.isEmpty(tvNavTitle.getText().toString())){
            int marginLeftRight = 2 * 16;
            if(tvNavBack.getVisibility() == VISIBLE || tvNavFinish.getVisibility() == VISIBLE){
                marginLeftRight = marginLeftRight + FINISH_ICON_WIDTH_DEFAULT;
            }
            if(tvNavShare.getVisibility() == VISIBLE){
                marginLeftRight = marginLeftRight + FINISH_ICON_WIDTH_DEFAULT + 16;
            }
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tvNavTitle.getLayoutParams();
            lp.setMargins(DensityUtil.dip2px(getContext(),marginLeftRight),0,DensityUtil.dip2px(getContext(),marginLeftRight),0);
            tvNavTitle.setLayoutParams(lp);
        }
    }

    public void setStatusBarSpaceShow(boolean isShow, int colorId){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            //android4.3及其以下版本，不做处理（因为状态栏透明可以被设置是从android4.4开始引进的,并且在5.0进行了改进）
            return;
        }
        if(isShow){
            vStatusBar.getLayoutParams().height = SystemUtil.getStatusHeight(getContext());
        }else{
            vStatusBar.getLayoutParams().height = 0;
        }

        vStatusBar.setBackgroundColor(colorId);
    }

    @Override
    public void setBackgroundColor(int color) {
        if(layNavMain!=null){
            layNavMain.setBackgroundColor(color);
        }
        super.setBackgroundColor(color);
    }

    public void setNavTitle(String string){
        tvNavTitle.setText(string);
        reSizeTitleViewWidth();
    }
}
